package com.syqc.monitor.ui.sms

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import autodispose2.autoDispose
import com.syqc.comlib.auto.AutoDisposeViewModel
import com.syqc.comlib.utils.CacheMMKV
import com.syqc.entity.SmsCmdInfo
import com.syqc.monitor.R
import com.syqc.net.ServiceHelper
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.subjects.PublishSubject

/**
 *@Author Jasper
 *@Time   2021/1/11
 *@Desc   短信指令
 */
class SMSCmdVieModel : AutoDisposeViewModel() {
    //收藏数据ID
    private var collectIds = mutableListOf<String>()

    //搜索数据
    private val likeData = mutableListOf<SmsCmdInfo>()

    //收藏数据
    private val collects = mutableListOf<SmsCmdInfo>()


    private val smsSub by lazy {
        PublishSubject.create<MutableList<SmsCmdInfo>>()
    }

    fun smsObserver(): Observable<MutableList<SmsCmdInfo>> = smsSub.hide()

    private val _collectNumLive = MutableLiveData<MutableList<SmsCmdInfo>>()
    val collectNumLive: LiveData<MutableList<SmsCmdInfo>> = _collectNumLive

    private var smsList: MutableList<SmsCmdInfo>? = null

    init {
        val ids = CacheMMKV.decodeString("SmsCmdIds", "")
        val split = ids.split(",")
        if (split.isNotEmpty()) {
            collectIds.addAll(split)
        }
    }

    fun getSMSCmd() {
        toastLoading(true)
        ServiceHelper.getRepository().getSMSCmd().autoDispose(this).subscribe({
            if (it.isNullOrEmpty()) {
                toast(R.string.desc_no_about_data)
                toastLoading(false)
                return@subscribe
            }
            toastLoading(false)
            smsList = it
            handleData()
        }, error)
    }


    //todo 此处有缺陷  不会根据数据改变 可能会造成数据错误
    private fun handleData() {
        smsList?.forEachIndexed { index, smsCmdInfo ->
            smsCmdInfo.index = index
            smsCmdInfo.isChoose = false
            smsCmdInfo.id = "A$index"
        }
        initCollect()
    }

    fun getAllTmn() = smsList

    fun getCollects() = collects

    //初始设置收藏
    private fun initCollect() {
        collects.clear()
        collectIds.forEach { item ->
            smsList?.forEach {
                if (it.id == item) {
                    it.isChoose = true
                    collects.add(it)
                }
            }
        }
        smsSub.onNext(smsList)

        collects.clear()
        smsList?.forEach {
            if (it.isChoose) collects.add(it)
        }
        _collectNumLive.postValue(collects)
    }

    //设置收藏
    fun setCollect(isCollect: Boolean, smsCmd: SmsCmdInfo) {
        if (isCollect) {
            collects.add(smsCmd)
            collectIds.add(smsCmd.id)
            saveIds()
            _collectNumLive.postValue(collects)
        } else {
            collects.forEach {
                if (it.id == smsCmd.id) {
                    collects.remove(it)
                    collectIds.remove(it.id)
                    _collectNumLive.postValue(collects)
                    saveIds()
                    return
                }
            }
        }
    }

    //全部
    fun setAllSms() {
        smsSub.onNext(smsList)
    }

    /**
     * 关键词查相似数据
     *
     * @param key
     * @return
     */
    fun getLikeDAta(key: String) {
        likeData.clear()
        val upKey = key.uppercase()
        smsList?.forEach {
            if (it.name.uppercase().contains(upKey)) likeData.add(it)
        }
        smsSub.onNext(likeData)
    }


    private fun saveIds() {
        var ids = ""
        collectIds.forEach {
            ids = "$ids,$it"
        }
        CacheMMKV.encode("SmsCmdIds", ids)
    }


}
